#! /usr/local/bin/python3

from tkinter import *
from collections import namedtuple

import Resign_ipa.ResignView


class LoginClass(Frame):
    '''
    登录注册界面
    '''
    def __init__(self, master=None):
        # __init__()这里是一个构造函数
        # 基本用法：Frame(根对象, [属性列表])
        Frame.__init__(self, master)
        self.master = master
        # 窗口大小和初始位置
        self.master.geometry('600x400')
        # 禁止用户调整尺寸
        self.master.resizable(0, 0)
        # 窗口标题
        self.master.title('登录')
        # 根据文本自适应窗口大小
        self.pack(fill=BOTH, expand=1)
        # 刷新窗口
        self.update()

        # 账号布局
        self.frame_user = Frame(self.master)
        self.value_user = Variable()
        # 密码布局
        self.frame_pw = Frame(self.master)
        self.value_pw = Variable()
        # 登录注册布局
        self.frame_login = Frame(self.master)

        # 创建界面
        self.view_login()

    def view_login(self):
        # 参照大小
        Size = namedtuple('Size', ['w', 'h'])
        f_size = Size(300, 30)
        # 参照坐标
        Point = namedtuple('Point', ['x', 'y'])
        r_point = Point((self.winfo_width()-300)/2, 150)

        # logo

        self.frame_user.config(width=f_size.w, height=f_size.h)
        # 账号
        Label(self.frame_user, text='账号:', fg='black', font=('', 15)).place(x=5, y=2)
        Entry(self.frame_user, textvariable=self.value_user).place(x=55, y=0, height=f_size.h, width=f_size.w-55)
        self.frame_user.place(x=r_point.x, y=r_point.y)

        self.frame_pw.config(width=f_size.w, height=f_size.h)
        # 密码
        Label(self.frame_pw, text='密码:', fg='black', font=('', 15)).place(x=5, y=2)
        Entry(self.frame_pw, show='*', textvariable=self.value_pw).place(x=55, y=0, height=f_size.h, width=f_size.w-55)
        self.frame_pw.place(x=r_point.x, y=r_point.y + f_size.h + 10)

        self.frame_login.config(width=f_size.w, height=f_size.h)
        # 注册
        Button(self.frame_login, text='注册', command=self.regist_click).place(x=25, y=0, width=100, height=f_size.h)
        # 登录
        Button(self.frame_login, text='登录', command=self.login_click).place(x=175, y=0, width=100, height=f_size.h)
        self.frame_login.place(x=r_point.x, y=r_point.y + f_size.h + 10 + f_size.h + 20)

    # 登录事件
    def login_click(self):
        print('登录')
        print('账号:%s 密码:%s' % (self.value_user.get(), self.value_pw.get()))
        self.pack_forget()
        ToolPageClass(self.master)

    # 注册事件
    def regist_click(self):
        print('注册')


class ToolPageClass(Frame):
    '''
    功能选择界面
    '''
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.master.title('选择功能')
        # 根据文本自适应窗口大小
        self.pack(fill=BOTH, expand=1)
        self.update()

        # 功能布局
        self.frame_tool = Frame(master)
        self.tools = ['重签名', '混淆', '更多功能', '返回']

        self.tool_list()

    def tool_list(self):
        # 容器大小
        Size = namedtuple('Size', ['w', 'h'])
        frame_size = Size(300, self.winfo_height() - 40)

        self.frame_tool.config(width=frame_size.w, height=frame_size.h)
        # 循环添加按钮
        for i in range(len(self.tools)):
            text = self.tools[i]
            btn = Button(self.frame_tool,
                   text=text,
                   command=lambda index=i, text=text: self.tool_click(index, text))
            btn.place(x=0, y=i*40, width=frame_size.w, height=30)
        # 显示容器
        self.frame_tool.place(x=(self.winfo_width()-300)/2, y=20)

    def tool_click(self, i, text):
        print('点击:', text)
        if i == len(self.tools) - 1:  # 返回
            self.pack_forget()
            LoginClass(self.master)
        elif i == 0:  # 重签名
            self.pack_forget()
            Resign_ipa.ResignView.ResignClass(self.master)


# 初始化一个Tk对象，Tk()这个类描述的是一个主窗体
rootView = Tk()
# 刷新窗体
# rootView.update()
# 获取窗口位于屏幕的坐标系
# print(rootView.winfo_x(), rootView.winfo_y())

# 把rootView这个顶层窗体作为一个对象传入参数到我们定义的Window类
app = LoginClass(rootView)
# 主消息循环
app.mainloop()

